package com.example.filecollectionsystemdemo1.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.filecollectionsystemdemo1.entity.Users;
import com.example.filecollectionsystemdemo1.exception.ServiceException;
import com.example.filecollectionsystemdemo1.mapper.UsersMapper;
import com.example.filecollectionsystemdemo1.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (Users)表服务实现类
 *
 * @author makejava
 * @since 2024-10-08 17:03:31
 */
@Service("usersService")
public class UsersService extends ServiceImpl<UsersMapper, Users>{
    @Resource
    UsersMapper usersMapper;

    @Override
    public boolean save(Users entity) {
        if(StrUtil.isBlank(entity.getName())){
            entity.setName(entity.getUsername());
        }
        if(StrUtil.isBlank(entity.getPassword())){
            entity.setPassword("123"); //默认密码
        }
        if(StrUtil.isBlank(entity.getRole())){
            entity.setRole("用户"); //默认角色
        }
        return super.save(entity);
    }

    public Users selectByUsername(String username) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Username", username);  //  eq => ==   where username = #{username}
        // 根据用户名查询数据库的用户信息
        return getOne(queryWrapper); //  select * from users where username = #{username}
    }

    // 验证用户账户是否合法
    public Users login(Users users) {
        Users dbUser = selectByUsername(users.getUsername());
        if (dbUser == null) {
            // 抛出一个自定义的异常
            throw new ServiceException("用户名或密码错误");
        }
        if (!users.getPassword().equals(dbUser.getPassword())) {
            throw new ServiceException("用户名或密码错误");
        }
        // 生成token
        String token = TokenUtils.createToken(dbUser.getUserid().toString(), dbUser.getPassword());
        dbUser.setToken(token);
        return dbUser;
    }

    public Users register(Users users) {
        Users dbUser = selectByUsername(users.getUsername());
        if (dbUser != null) {
            // 抛出一个自定义的异常
            throw new ServiceException("用户名已存在");
        }
        users.setName(users.getUsername());
        users.setRole("学生");
        usersMapper.insert(users);
        return users;
    }

    public void resetPassword(Users users) {
        Users dbUser = selectByUsername(users.getUsername());
        if (dbUser == null) {
            // 抛出一个自定义的异常
            throw new ServiceException("用户不存在");
        }
        if (!users.getPhone().equals(dbUser.getPhone())) {
            throw new ServiceException("验证错误");
        }
        dbUser.setPassword("123");   // 重置密码
        updateById(dbUser);
    }

    // 获取当前登录用户信息
    public  Users getCurrentUser() {
         return TokenUtils.getCurrentUser();
    }
}


